using UnityEngine;
using System.Collections;
using System;

/// Vungle广告
public class VungleAds : AdsBase
{

	public VungleAds ()
	{
		Vungle.onAdStartedEvent += onAdStartedEvent;
		Vungle.onAdFinishedEvent += onAdFinishedEvent;
		Vungle.adPlayableEvent += adPlayableEvent;
	}
	
	public override void Init (params string[] key)
	{
		TJLog ("初始化 Android key: " + key [0] + ",iOS key: " + key [1]);
		Vungle.init (key [0], key [1]);
		Vungle.setSoundEnabled (false);
	}
	
	public override void ShowIncent (Action<bool> ac)
	{
		TJLog ("展示激励视频");
		incentCallBack = ac;
		Vungle.playAd (true, "user-tag");
	}
	
	public override void ShowInsertVideo (Action<bool> ac)
	{
		insertCallBack = ac;
		ShowInsert (insertCallBack);
	}
	
	public override void ShowInsert (Action<bool> ac)
	{
		TJLog ("展示插屏视频");
		Vungle.playAd ();
	}

	public override void OnADCallback (string ev)
	{
		if (ev.Contains ("complete")) {
			TJLog ("视频/插屏 播放完成");
			if (incentCallBack != null){
				incentCallBack (true);
			}
		} else if (ev.Contains ("uncomplete")) {
			TJLog ("视频/插屏 播放未完成，可能由于中途退出");
			if (incentCallBack != null){
				incentCallBack (false);
			}
		}
	}
	
	#region 判断视频和插屏是否加载好

	public override bool IsIncentReady ()
	{
		
		if (Vungle.isAdvertAvailable ()) {
			TJLog ("视频加载好啦");
			return true;
		} else {
			TJLog ("视频没加载好");
			return false;
		}
	}
	
	public override bool IsInsertReady ()
	{
		if (Vungle.isAdvertAvailable ()) {
			TJLog ("插屏加载好啦");
			return true;
		} else {
			TJLog ("插屏没加载好");
			return false;
		}
	}
	
	#endregion
	
	void onAdStartedEvent ()
	{
		TJLog ("广告开始事件");
	}
	
	void onAdFinishedEvent (AdFinishedEventArgs arg)
	{
		TJLog ("激励视频是否完成: " + arg.IsCompletedView);
		if (arg.IsCompletedView) {
			OnADCallback ("complete");
		}else{
			OnADCallback ("uncomplete");
		}
	}
	
	
	void adPlayableEvent (bool playable)
	{
		TJLog ("广告是否可用: " + playable);
	}
}
